﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShareMarking.Systems.Service;
using ShareMarking.Service;
using ShareMarking.Entity;
using System.Configuration;

namespace ShareMarking.Web.admin.EnterPrise.Activity
{
    public partial class Action : ManagePage
    {
        #region 属性=====================================

        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_Name
        {
            get { return txt_A_Name.Text; }
            set { txt_A_Name.Text = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_Desc
        {
            get { return txt_A_Desc.Text; }
            set { txt_A_Desc.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_Manager
        {
            get { return txt_A_Manager.Text; }
            set { txt_A_Manager.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_wxTitle
        {
            get { return txt_A_wxTitle.Text; }
            set { txt_A_wxTitle.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_wxImgUrl
        {
            get { return txt_A_wxImgUrl.Text; }
            set { txt_A_wxImgUrl.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_wxDesc
        {
            get { return txt_A_wxDesc.Text; }
            set { txt_A_wxDesc.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        protected String Par_A_wxUrl
        {
            get { return txt_A_wxUrl.Text; }
            set { txt_A_wxUrl.Text = value; }
        }

        protected String Par_A_wxbeforeUrl
        {
            get { return txtbeforeurl.Text; }
            set { txtbeforeurl.Text = value; }
        }

        protected String Par_A_wxafterUrl
        {
            get { return txtafterurl.Text; }
            set { txtafterurl.Text = value; }
        }

        protected String parsacode
        {
            get { return txtsacode.Text; }
            set { txtsacode.Text = value; }
        }

        protected DateTime  ParStartTime
        {
            get { return txtStartTime.Text.ConvertTo<DateTime>(); }
            set { txtStartTime.Text = value.ToString("yyyy-MM-dd HH:mm:ss"); }
        }

        protected DateTime ParEndTime
        {
            get { return txtEndTime.Text.ConvertTo<DateTime>(); }
            set { txtEndTime.Text = value.ToString("yyyy-MM-dd HH:mm:ss"); }
        }


        #endregion

        #region 初始化===================================

        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!qr_ActivityService.Instance.Get_IsExist_ByWhere(qr_Activity._.ActivityID == this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("user_list", ShareMarkingEnums.ActionEnum.View.ToString()); //检查权限

                //绑定下拉框

                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #endregion

        #region 绑定下拉框===============================


        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var entity = qr_ActivityService.Instance.Get_SingleEntity_byWhere(qr_Activity._.ActivityID == _id);
            if (entity == null) return;
            Par_A_Name = entity.A_Name;
            Par_A_Desc = entity.A_Desc;
            Par_A_Manager = entity.A_Manager;
            Par_A_wxTitle = entity.A_wxTitle;
            Par_A_wxImgUrl = entity.A_wxImgUrl;
            this.iImagesurl.ImageUrl = entity.A_wxImgUrl;
            Par_A_wxDesc = entity.A_wxDesc;
            Par_A_wxUrl = entity.A_wxUrl;
            Par_A_wxbeforeUrl = entity.A_wxbeforeUrl;
            Par_A_wxafterUrl = entity.A_wxafterUrl;
            parsacode = entity.A_SaCode;
            ParStartTime = entity.A_StartTime.ConvertTo<DateTime>();
            ParEndTime = entity.A_EndTime.ConvertTo<DateTime>();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            var httpstr = ConfigurationManager.AppSettings["vigohttp"];
            var signsecret = ConfigurationManager.AppSettings["signscrect"];
            var result=false;
            #region 添加活动二维码

            var currenturl = "";
            if (DateTime.Now < ParStartTime)
            {
                currenturl = Par_A_wxbeforeUrl;
            }
            else if (DateTime.Now > ParStartTime && DateTime.Now < ParEndTime)
            {
                currenturl = Par_A_wxUrl;
            }
            else
            {
                currenturl = Par_A_wxafterUrl;
            }

            var ssacode = "";
            if (currenturl.Contains("?"))
            {
                ssacode = "$sacode=" + parsacode;
            }
            else
            {
                ssacode = "?sacode=" + parsacode;
            }

            #region 组装消息
            var sceneid = 0;
            qr_QrCodeType type = qr_QrCodeTypeService.Instance.Get_SingleEntity_byWhere(qr_QrCodeType._.QrCodeTypeID == 1000);
            sceneid = type.QT_CurrentIndex.ConvertTo<int>() + 1;

            var extra = new List<ShareMarking.Interface.extra>() { 
                new ShareMarking.Interface.extra { 
                    title = Par_A_wxTitle, 
                    shortdesc = Par_A_wxDesc, 
                    linkurl = (currenturl+ssacode).Replace('&','$'),
                    picurl ="http://admin.mjn.c2d.me/"+ Par_A_wxImgUrl
                }
            };
            var message = new ShareMarking.Interface.message()
            {
                title = Par_A_Name,
                remark = Par_A_Name + "的二维码",
                sacode = "test",
                scene_id = sceneid.ToString(),
                extra = extra
            };
            #endregion
            
            #region vigo接口申请
            var vigo = new ShareMarking.Interface.Qrcode_vigo(httpstr, signsecret);
            var resultentity =vigo.CreateQrcode(message);
            #endregion            

            if (resultentity.errcode == "00")
            {
                #region 保存二维码到数据库
                qr_QrCode qrcode = new qr_QrCode()
                {
                    id = resultentity.message.id,
                    type = 1000,
                    scene_id = resultentity.message.scene_id,
                    sacode = resultentity.message.sacode,
                    title = resultentity.message.title,
                    remark = resultentity.message.remark,
                    code_url = resultentity.message.code_url,
                    qrcode_url = resultentity.message.qrcode_url,
                    addtime = resultentity.message.addtime,
                    status = resultentity.message.status,
                    isdelete = false
                };
                qr_QrCodeService.Instance.Add_Entity(qrcode);
                #endregion

                #region 保存活动
                var entity = new qr_Activity();
                entity.A_Name = Par_A_Name;
                entity.A_Desc = Par_A_Desc;
                entity.A_Manager = Par_A_Manager;
                entity.A_wxQrCodeID = resultentity.message.id.ToString();
                entity.A_wxSceneID = resultentity.message.scene_id.ToString();
                entity.A_wxTitle = Par_A_wxTitle;
                entity.A_wxImgUrl = Par_A_wxImgUrl;
                entity.A_wxDesc = Par_A_wxDesc;
                entity.A_wxUrl = Par_A_wxUrl;
                entity.A_wxbeforeUrl = Par_A_wxbeforeUrl;
                entity.A_wxafterUrl = Par_A_wxafterUrl;
                entity.A_SaCode = parsacode;
                entity.A_StartTime = ParStartTime;
                entity.A_EndTime = ParEndTime;
                entity.CreateOn = DateTime.Now;
                entity.isneedupdate = false;
                entity.A_currentUrl = currenturl;//当前时间的currurl
                qr_ActivityService.Instance.Add_Entity(entity);
                #endregion

                #region 添加关联
                qr_QrCodeRelate qrrelate = new qr_QrCodeRelate()
                {
                    qr_Type = 1000,//1000是活动
                    qr_CodeID = resultentity.message.id,
                    qr_Scene_ID = resultentity.message.scene_id,
                    rel_Code = entity.ActivityID.ToString(),
                    IsDelete = false,
                    CreateOn = DateTime.Now
                };
                qr_QrCodeRelateService.Instance.Add_Entity(qrrelate);
                #endregion

                #region 更改二维码类型表
                type.QT_CurrentIndex = sceneid;
                type.QT_Used = type.QT_Used + 1;
                type.QT_Surplus = type.QT_Surplus - 1;
                result = qr_QrCodeTypeService.Instance.Update_Entity(type);
                #endregion
            }
            #endregion

            if (result)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加qr_Activity"); //记录日志
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            var entity = qr_ActivityService.Instance.Get_SingleEntity_byWhere(qr_Activity._.ActivityID == _id);
            if (entity == null) return false;

            #region 修改活动二维码
            var httpstr = ConfigurationManager.AppSettings["vigohttp"];
            var signsecret = ConfigurationManager.AppSettings["signscrect"];
            var result = false;

            #region 组装消息

            var currenturl = "";
            if (DateTime.Now < ParStartTime)
            {
                currenturl = Par_A_wxbeforeUrl;
            }
            else if (DateTime.Now > ParStartTime && DateTime.Now < ParEndTime)
            {
                currenturl = Par_A_wxUrl;
            }
            else
            {
                currenturl = Par_A_wxafterUrl;
            }

            var ssacode = "";
            if (currenturl.Contains("?"))
            {
                ssacode = "$sacode=" + parsacode;
            }
            else
            {
                ssacode = "?sacode=" + parsacode;
            }

            var extra = new List<ShareMarking.Interface.extra>() { 
                new ShareMarking.Interface.extra { 
                    title = Par_A_wxTitle, 
                    shortdesc = Par_A_wxDesc, 
                    linkurl = (currenturl+ssacode).Replace('&','$'),
                    picurl ="http://admin.mjn.c2d.me/"+ Par_A_wxImgUrl
                }
            };
            var message = new ShareMarking.Interface.message2()
            {
                id = entity.A_wxQrCodeID,
                title = Par_A_Name,
                remark = Par_A_Name + "的二维码",
                sacode = "test",
                extra = extra
            };
            #endregion

            #region vigo接口申请
            var vigo = new ShareMarking.Interface.Qrcode_vigo(httpstr, signsecret);
            var resultentity = vigo.UpdateQrcode(message);
            #endregion

            #region 保存活动
            entity.A_Name = Par_A_Name;
            entity.A_Desc = Par_A_Desc;
            entity.A_Manager = Par_A_Manager;
            entity.A_wxTitle = Par_A_wxTitle;
            entity.A_wxImgUrl = Par_A_wxImgUrl;
            entity.A_wxDesc = Par_A_wxDesc;
            entity.A_wxUrl = Par_A_wxUrl;
            entity.A_wxbeforeUrl = Par_A_wxbeforeUrl;
            entity.A_wxafterUrl = Par_A_wxafterUrl;
            entity.A_SaCode = parsacode;
            entity.A_StartTime = ParStartTime;
            entity.A_EndTime = ParEndTime;
            entity.A_currentUrl = currenturl;//当前使用的URL
            result = qr_ActivityService.Instance.Update_Entity(entity);
            #endregion

            #endregion

            if (result)
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改qr_Activity:" + entity.ActivityID); //记录日志
            }
            return result;
        }
        #endregion

        #region 保存按钮事件=============================

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString())
            { //修改
                //ChkAdminLevel("user_list", ShareMarkingEnums.ActionEnum.Edit.ToString()); //检查权限
                //if (!DoEdit(this.id))
                //{
                //    JscriptMsg("保存过程中发生错误！", "", "Error");
                //    return;
                //}
                DoEdit(this.id);
                JscriptMsg("修改成功！", "List.aspx", "Success");
            }
            else //添加
            {
                //ChkAdminLevel("user_list", ShareMarkingEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "", "Error");
                    return;
                }
                JscriptMsg("添加成功！", "List.aspx", "Success");
            }
        }

        #endregion
    }

}